package com.yangyao.microserviceoauth2.common.entity;

import com.yangyao.microserviceoauth2.common.constants.StatusCodeConstant;
import com.yangyao.microserviceoauth2.common.constants.StatusMessageConstant;
import lombok.Data;

import java.io.Serializable;

/**
 * @author yangyao
 * @Description: Result响应给前端的对象信息
 * @date 2019/1/23 14:04
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -8561468745103971131L;
    private Integer code;

    private String msg;

    private transient T data;

    /**
     * 操作失败
     * @param msg
     * @return
     */
    public static Result error(String msg) {
        Result result = new Result();
        result.setCode(StatusCodeConstant.ERROR);
        result.setMsg(msg);
        return result;
    }

    /**
     * 操作成功，不返回数据
     * eg增删改
     *
     * @return
     */
    public static Result ok() {
        Result result = new Result();
        result.setCode(StatusCodeConstant.SUCCESS);
        result.setMsg(StatusMessageConstant.SUCCESS);
        return result;
    }

    /**
     * 操作成功，返回数据
     * eg查询
     *
     * @return
     */
    public static Result ok(Object t) {
        Result result = new Result();
        result.setCode(StatusCodeConstant.SUCCESS);
        result.setMsg(StatusMessageConstant.SUCCESS);
        result.setData(t);
        return result;
    }
}
